package com.hzcx.app.simplechat.ui.group;

import androidx.annotation.MainThread;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

import com.github.promeg.pinyinhelper.Pinyin;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.chat.ChatGroupSettingActivity;
import com.hzcx.app.simplechat.ui.friend.FriendListByLableActivity;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.adapter.GroupMembersSort;
import com.hzcx.app.simplechat.ui.group.adapter.GroupMuteMemberAdapter;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.util.PinyinUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GroupMuteSettingActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.setting_group_all_mute)
    Switch groupAllMute;
    @BindView(R.id.setting_group_mute_members_list)
    RecyclerView groupMuteMembersList;

    GroupMuteMemberAdapter adapter;
//    public static String GroupInfoBeanKey = "GroupMuteSettingActivityGroupInfoBean";
//    GroupInfoBean groupInfoBean;

    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;

    List<MembersBean> beans;

    @Override
    public int getLayout() {
        return R.layout.activity_group_mute_setting;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("群禁言");
    }

    @Override
    protected void onResume() {
        super.onResume();

        //        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(GroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);

        groupAllMute.setChecked(groupInfoBean.isMute());

        beans = new ArrayList<>();
        for (MembersBean membersBean: groupInfoBean.getMembers()) {
            if (membersBean.getIs_mute()) {
                beans.add(membersBean);
            }
        }
//        adapter = new GroupMuteMemberAdapter(beans, null);
        adapter = new GroupMuteMemberAdapter(null, beans);
        adapter.onItemClickListener = new GroupMuteMemberAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(FriendCityBean bean) { }
            @Override
            public void onItemClick(MembersBean bean) {
                showLoading();
                GroupModel.relieveMuteGroupMembres(GroupMuteSettingActivity.this, groupInfoBean.getGroup_id(), String.valueOf(bean.getMember_id()), new BaseObserver<BaseEmptyBean>() {
                    @Override
                    protected void onSuccess(BaseEmptyBean data) {
                        hideLoading();
                        beans.remove(bean);
                        for (MembersBean membersBean: groupInfoBean.getMembers()) {
                            if (membersBean.getMember_id() == bean.getMember_id()) {
                                membersBean.setIs_mute(false);
                                break;
                            }
                        }
                        showError("解禁成功");
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        };
        groupMuteMembersList.setAdapter(adapter);
        groupMuteMembersList.setLayoutManager(new LinearLayoutManager(this));
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.setting_group_all_mute, R.id.setting_group_mute_members})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.setting_group_all_mute:
                if (groupAllMute.isChecked()) {
                    muteGroup();
                } else {
                   relieveMuteGroup();
                }
                break;
            case R.id.setting_group_mute_members:
                List<FriendBean> beans = GroupMembersSort.sortFriend(groupInfoBean.getMembers());
                Intent intent = new Intent(GroupMuteSettingActivity.this, ChatGroupMuteMemberActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(FriendListByLableActivity.INTENT_LIST_BUNDLE, beans.toArray());
                intent.putExtras(bundle);
                intent.putExtra(GroupCreateActivity.GroupCreateActivityGroupID, groupInfoBean.getGroup_id());
                startActivity(intent);
                break;
        }
    }

    private void muteGroup() {
        GroupModel.muteGroup(this, groupInfoBean.getGroup_id(), new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                EMClient.getInstance().groupManager().muteAllMembers(groupInfoBean.getHx_groupid(), new EMValueCallBack<EMGroup>() {
                    @Override
                    public void onSuccess(EMGroup emGroup) {
                        groupInfoBean.setMute(true);
                        showError("全员禁言成功");
                    }
                    @Override
                    public void onError(int i, String s) {
                        showError("全员禁言失败");
                    }
                });
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                showError("全员禁言失败");
            }
        });
    }

    private void relieveMuteGroup() {
        GroupModel.relieveMuteGroup(this, groupInfoBean.getGroup_id(), new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                EMClient.getInstance().groupManager().unmuteAllMembers(groupInfoBean.getHx_groupid(), new EMValueCallBack<EMGroup>() {
                    @Override
                    public void onSuccess(EMGroup emGroup) {
                        groupInfoBean.setMute(false);
                        showError("取消全员禁言成功");
                    }
                    @Override
                    public void onError(int i, String s) {
                        showError("取消全员禁言失败");
                    }
                });
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
//                super.onFailure(e, errorMsg);
                showError("取消全员禁言失败");
            }
        });
    }
}